﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Text.RegularExpressions;
using System.Web;
using System.Net;
using System.Configuration;
using System.Collections.Specialized;
using System.Net.Mail;
using System.Globalization;

namespace Dln.Common
{
    public class CommonHelper
    {
        #region 配置文件
        public static String GetAppSettings(String KeyName)
        {
            return ConfigurationManager.AppSettings[KeyName] != null ? ConfigurationManager.AppSettings[KeyName].ToString() : "";
        }
        #endregion
        #region 生成随机号
        /// <summary>
        /// 生成随机号
        /// </summary>
        /// <param name="length">长度</param>
        /// <returns></returns>
        public static string GenRandomNumber(int length)
        {
            string[] source = null;
            string code = "";
            source = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9" };
            Random rd = new Random();
            for (int i = 0; i < length; i++)
            {
                code += source[rd.Next(0, source.Length)];
            }
            return code;
        }
        #endregion

        /// <summary>
        /// 插入测试消息记录
        /// </summary>
        /// <param name="info"></param>
        public static void InsertTextMessage(string info)
        {
        }

        #region DataGridParam
        /// <summary>
        /// 获取post传值，并返回DataGridParam类型
        /// </summary>
        /// <param name="postValueCollection"></param>
        /// <returns></returns>
        public static DataGridParam GetDataGridParam(NameValueCollection postValueCollection)
        {
            var paramValues = new DataGridParam();
            paramValues.Page = Convert.ToInt32(postValueCollection["Page"].Split(',')[0]);
            paramValues.RP = Convert.ToInt32(postValueCollection["RP"]);
            paramValues.SortName = postValueCollection["sort"];
            paramValues.SortOrder = postValueCollection["order"];
            if (!string.IsNullOrEmpty(postValueCollection["Query"]))
            {
                paramValues.Query = postValueCollection["Query"];
            }
            return paramValues;
        }
        #endregion

        #region 将String转换为Dictionary类型
        /// <summary>
        /// 将String转换为Dictionary类型，过滤掉为空的值，首先 6 分割，再 7 分割:&
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public static Dictionary<string, string> StringToDictionary(string value)
        {
            if (!string.IsNullOrEmpty(value))
            {
                Dictionary<string, string> queryDictionary = new Dictionary<string, string>();
                string[] s = value.Split('^');
                for (int i = 0; i < s.Length; i++)
                {
                    if (!string.IsNullOrWhiteSpace(s[i]) && !s[i].Contains("undefined") && !s[i].Contains("null"))
                    {
                        var ss = s[i].Split('&');
                        if ((!string.IsNullOrEmpty(ss[0])) && (!string.IsNullOrEmpty(ss[1])))
                        {
                            queryDictionary.Add(ss[0], ss[1]);
                        }
                    }

                }
                return queryDictionary;
            }
            return null;
        }
        #endregion

        #region 验证是否包含中文
        public static bool IsNumeric(string value)
        {
            return Regex.IsMatch(value, @"[\u4e00-\u9fa5]");
        }
        #endregion

        #region 截断字符串

        public static string StringTruncat(string oldStr, int maxLength, string endWith)
        {
            // 判断原字符串是否为空
            if (string.IsNullOrEmpty(oldStr))
                return oldStr + endWith;


            // 返回字符串的长度必须大于 1
            if (maxLength < 1)
                throw new Exception("返回的字符串长度必须大于 [0] ");


            // 判断原字符串是否大于最大长度
            if (oldStr.Length > maxLength)
            {
                // 截取原字符串
                string strTmp = oldStr.Substring(0, maxLength);


                // 判断后缀是否为空
                if (string.IsNullOrEmpty(endWith))
                    return strTmp;
                else
                    return strTmp + endWith;
            }
            return oldStr;
        }
        #endregion

        public static string RandString()
        {
            string str = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";//62个字符
            Random r = new Random();
            string result = string.Empty;

            //生成一个8位长的随机字符，具体长度可以自己更改
            for (int i = 0; i < 16; i++)
            {
                int m = r.Next(0, 62);//这里下界是0，随机数可以取到，上界应该是75，因为随机数取不到上界，也就是最大61，符合我们的题意
                string s = str.Substring(m, 1);
                result += s;
            }

            return result;
        }

        public static string SHA1(string text)
        {
            byte[] cleanBytes = Encoding.Default.GetBytes(text);
            byte[] hashedBytes = System.Security.Cryptography.SHA1.Create().ComputeHash(cleanBytes);
            return BitConverter.ToString(hashedBytes).Replace("-", "");
        }


    }
}
